import os
import wx
import time
import re
from wx import ImageFromStream, BitmapFromImage, EmptyIcon
import cStringIO, zlib
#import psyco
#psyco.log()
#psyco.profile()

ID_NEW  = 100
ID_OPEN = 101
ID_SAVE = 102
ID_SAVEAS = 103
ID_EXIT = 104

ID_CUT = 105
ID_COPY = 106
ID_PASTE = 107
ID_DEL = 108
ID_SELECTALL = 109 
ID_TSTAMP = 110

ID_FONT = 111

ID_ABOUT = 112
ID_CURSOR = 113

ID_SEARCH = 114
ID_SEARCH_NEXT = 115
ID_REPLACE = 116

ID_TOOL = 121

ID_STATUSBAR_TOGGLE = 117#//
ID_TOOLBAR_TOGGLE = 118#// 
ID_SEARCH_SEARCH_TOGGLE = 119#// 
ID_SEARCH_CANCEL_TOGGLE = 120#// 
#// ID_SEARCH_HISTORY_TOGGLE =

def getData():
    return zlib.decompress(
'x\xda\x01\\\n\xa3\xf5\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00 \x00\
\x00\x00 \x08\x06\x00\x00\x00szz\xf4\x00\x00\x00\x04sBIT\x08\x08\x08\x08|\
\x08d\x88\x00\x00\n\x13IDATX\x85\xb5\x97m\x8c\x1d\xd7Y\xc7\x7fs\xce\xbc\xdf{\
g\xe7\xde}_\xaf\xd7\xd7\xb1l\'^;q\xec\xa41\xa4n\x12\xda\xaa\xa6\xa1u[\xb9\
\xfd\x00E\x14!\x02B\x02\xa9\x12\x88\x8aP$\xa0\x95R\x10\xe0\x08\xa9EU\x83\x82\
\xaa@\xd4\n\x1a\xe2T\xad\xd24I\x93\xa6MZ71\x89\xed.^\xd7^o\xf6\xe5\xee\xfb}\
\x9d;w\xde\x0e\x1f\xe6f\xd7EH\x88\x0f\x1c\xe9\xe8\xcc\xd1\x9c\x99\xf3?\xff\
\xe7\xff<\xcfy4@\x01|\xe8\xcc\x19\xce|\xf2\x93\x14\x87\x86X\x9c\x9fg\xbb\xa5\
\xf9\xd0\n\xa2|\xec\xf5\x00\xe8\x05\xad|\xde\xea\xf5\x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xce\xb2\xeb\x9e\
\xd3\xc4A\x93\xda+\xdfd\xea\xc1O\x11\x07\xb90\xa5\xed!\xec\x12\xc20\x10\xc66\
\xc1pS\x1c\x08j;\xe9^x\xe5\x12\x00\xbdp\x95^w\x1d\xd5\r \x0cQ\xb1BS\x02l\x1b\
Q\x1c\xc0r\x071\x0cg\xfb\xc3\x95\x9f\xbc\xc6\xde\xf7?D\xe4\xaf\xb1\xd5x\x85\
\xf1\x93\xef\'n\xd6\x01\x1b\xc3\x18\xc40\\L\xcb@J\x89\x14\x12$\x08\xd3\xc1\
\xb6}D\x9c\x81n\xb3V\xab\xedh@\xa5I~R\x05B\x17\x08\xcbD\x93\x12\xad0\x04Jq\
\xc7\xef\xff\x15f\xc9\'i\x06l\xbe\xf9*\xcbo\xbc\x84\xe9\xf98\xc3\xbbHz\x01C\
\xfb\x0e\x90t\xda\xac\xbc\xf1\n\x986\xba\xe3\xa1\xb2\x14\x91\x85\xa4q\x8a\
\xe1\x940]\x0f\x00\xef\xe0q\xea\x0b3\xb9\x06\xccR\x1e\x08\x940\xc10r\xa6t\
\x13\xdd\xc9\x17\xff\xe2\x9f\x7f\x91\xfa\xec\x15\xccr\xca\xd2\x85\'1<\x9b\
\xc2\xdd\x13\xdc\xf9\xc0\x1fb\x15+l.;\xe8n\x91\xb5\xb5\x17\xa9~\xe2\x83D\xb5\
U\xbaK\xcbd\xd6\x10RA\xc6\x16\xba\x9eBi\x14\x14\xecz\xf0A\x92\xa0\xdd\xe7\
\x9f\x1d\r\x08\xa3\x80n\x95\x11\xa6\x8dQ(\x02\xe0\x8e\xed\xa7\xb3\xbc\x04(\
\xea\xf3\x970\x8b\xe5\xdc\x86+K\xac\xfc\xe7\xf7\xa9\xcd\xbc\x8c&u\xa4eS\x1c\
\x9d\xa4~c\x96\xfd\xbf\xfb\xdb\xe8\xa5"BZh\xc2\x04\xa1\xe5\x1d0J\x13\x0cN\
\x1f\xa79w\xe5f/\xc8\xdd\xc0.O\x11\x05\rDb\x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>v\x86\xa8\xd5&\xd9X\xc5\xda5\xccj\
\xf3\xb9>\x80\xbc\xd8L[\xb9\t\xddb\x91\xb1c\x1fa\xe1\xb9\xa7H\x82\x80\xb4\
\x11\x92\xf4"\xe8\x85\xa84\xe3\xd7\xfe\xe0\xd3<\xfd\xf8?\xe6,\x8fU\xf7\xa9\
\x13\xa7>\xac*~\xf9\x7f/0u\xfb\xe7\xfb\xff\xb18=\xf5\xd13\xea\x8f\xfe\xfaQu`\
\xfav\x05(\xad\xff\x82\x92_f\xea\xd6iJ\xe3\x93H]\xe2\x15r&,7\x8fr%\xaf\xd4\
\xb7a\xae\x19\xd3\xcec\xc3Xu2\x17T_\xcc$}\x17\x8b\xf2\xf9N9\x1f\xd1n\xd4\xe9\
,/\xf0\xc6+/\xd1l6\x01\xf8/I\x94i5~\x95\x1d\xc9\x00\x00\x00\x00IEND\xaeB`\
\x82:\xdb\x177' )

def getBitmap():
    return BitmapFromImage(getImage())

def getImage():
    stream = cStringIO.StringIO(getData())
    return ImageFromStream(stream)

class MainWindow(wx.Frame):
	def __init__(self, parent, id, title):
		wx.Frame.__init__(self, parent, wx.ID_ANY, title, size = (600,600), style = wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE)
		self.control = wx.TextCtrl(self, wx.ID_ANY, style = wx.TE_MULTILINE)
		
		# initialized variables
		self.FileOpen = 0 # Has the a file been open, otherwise use saveas
		self.dirname = ''
		self.filename = ''
		self.OldPosTo = -1
		
		# Draw a status bar
		self.statusbar = self.CreateStatusBar()
		self.StatusBar.SetFieldsCount(3)
		self.StatusBar.SetStatusWidths([-5, -1, -2])
		
		# Big O - Its show time!  Update the Status bar time once per second
		self.timer = wx.PyTimer(self.StatusBarShowTime)
		self.timer.Start(1000)
		self.StatusBarShowTime()
		
		# File Menu
		fileMenu = wx.Menu()
		fileMenu.Append(ID_NEW, "&New...\tCtrl+N", "Open a new editor window...")
		fileMenu.Append(ID_OPEN, "&Open...\tCtrl+O", "Open a file...")
		fileMenu.Append(ID_SAVE, "&Save\tCtrl+S", "Save your current file")
		fileMenu.Append(ID_SAVEAS, "S&ave As...", "Save as a new file... ")
		fileMenu.AppendSeparator()
		fileMenu.Append(ID_EXIT, "E&xit\tCtrl+Q", "Exit")
		
		# Edit menu
		editMenu = wx.Menu()
		editMenu.Append(ID_CUT, "Cut\tCtrl+X", "Cut selection")
		editMenu.Append(ID_COPY, "Copy\tCtrl+C", "Copy selection")
		editMenu.Append(ID_PASTE, "Paste\tCtrl+V", "Paste previously cut or copied information")
		editMenu.Append(ID_DEL, "Delete", "Delete selection")
		editMenu.AppendSeparator()
		editMenu.Append(ID_SEARCH, "Find...\tCtrl+F", "Find in current file") #// Bind
		editMenu.Append(ID_SEARCH_NEXT, "Find Next\tF3", "Find next value in current file") #// Bind
		editMenu.Append(ID_REPLACE, "Replace...\Ctrl+R", "Find in current file and if found replace it with a different value") #// Bind
		editMenu.AppendSeparator()
		editMenu.Append(ID_SELECTALL, "Select All\tCtrl+A", "Select All")
		editMenu.Append(ID_TSTAMP, "Time/Date Stamp\tF5", "Inserts the Time and Date at the end of the file")
		
		# Format Menu
		formatMenu = wx.Menu()
		formatMenu.Append(ID_FONT, "&Font", "Change font settings")
		
		# Options Menu
		optionsMenu = wx.Menu()
		self.sb_tog = optionsMenu.Append(ID_STATUSBAR_TOGGLE, "Status Bar", "Toggle the status bar on and off", kind = wx.ITEM_CHECK)
#		self.sb_tog.SetValue(True)
		self.tb_tog = optionsMenu.Append(ID_TOOLBAR_TOGGLE, "Toolbar", "Toggle the toolbar on and off", kind = wx.ITEM_CHECK)
#		self.tb_tog.SetValue(False)
		optionsMenu.AppendSeparator()
#//		optionsMenu.Append(ID_SEARCH_SEARCH_TOGGLE, "Search Button", "Shows or hides the search button on the search box", kind = wx.ITEM_CHECK)
#//		optionsMenu.Append(ID_SEARCH_CANCEL_TOGGLE, "Cancel Button", "Shows or hides the cancel button on the search box", kind = wx.ITEM_CHECK)
#//		optionsMenu.Append(ID_SEARCH_HISTORY_TOGGLE

		# Tools
		toolMenu = wx.Menu()
		toolMenu.Append(ID_TOOL, "&Palindrome Test", "Tests the current text to see if it is a palindrome")
		
		# Help Menu
		helpMenu = wx.Menu()
		helpMenu.Append(ID_ABOUT, "&About", "About...")
		
		# Set the buttons in their respective order in the program
		menubar = wx.MenuBar()
		menubar.Append(fileMenu, "&File")
		menubar.Append(editMenu, "&Edit")
		menubar.Append(formatMenu, "For&mat")
#//		menubar.Append(optionsMenu, "&Options")
		menubar.Append(toolMenu, "&Tools")
		menubar.Append(helpMenu, "&Help")
		
		# Draw the menu bar
		self.SetMenuBar(menubar)
		
		
		# I knew these ones worked ----------------------------------
		# wx.EVT_MENU(self, ID_ABOUT, self.OnAbout)
		# wx.EVT_MENU(self, ID_SAVE, self.OnSave)
		# wx.EVT_MENU(self, ID_SAVEAS, self.OnSaveAs)
		# wx.EVT_MENU(self, ID_EXIT, self.OnExit)
		# wx.EVT_MENU(self, ID_OPEN, self.OnOpen)
		# wx.EVT_MENU(self, ID_FONT, self.OnSetFont)
		# wx.EVT_MENU(self, ID_TSTAMP, self.TimeStamp)
		#----------------------------------------------------------------------
		
		# Events
		wx.EVT_MENU(self, ID_NEW, self.OnNewDoc)
		wx.EVT_MENU(self, ID_OPEN, self.OnOpen)
		wx.EVT_MENU(self, ID_SAVE, self.OnSave)
		wx.EVT_MENU(self, ID_SAVEAS, self.OnSaveAs)
		wx.EVT_MENU(self, ID_EXIT, self.OnExit)

		wx.EVT_MENU(self, ID_CUT, self.OnCut)
		wx.EVT_MENU(self, ID_COPY, self.OnCopy)
		wx.EVT_MENU(self, ID_PASTE, self.OnPaste)
		wx.EVT_MENU(self, ID_DEL, self.OnDelete)
		wx.EVT_MENU(self, ID_SELECTALL, self.OnSelectAll)

		wx.EVT_MENU(self, ID_TSTAMP, self.TimeStamp)
		wx.EVT_MENU(self, ID_FONT, self.OnSetFont)

		wx.EVT_MENU(self, ID_ABOUT, self.OnAbout)
		wx.EVT_MENU(self, ID_STATUSBAR_TOGGLE, self.StatusBarToggle)
		wx.EVT_MENU(self, ID_TOOLBAR_TOGGLE, self.ToolBarToggle)
		wx.EVT_MENU(self, ID_TOOL, self.OnPalindromeTest)
		
		wx.EVT_KEY_UP(self.control, self.PositionUpdate)
		#wx.EVT_LEFT_DOWN(self.control, self.PositionUpdate)
		
		icon = wx.EmptyIcon()
		icon.CopyFromBitmap(getBitmap())
		self.SetIcon(icon)
		
		#// Search box (from the demo)

		#// Graphical Toolbar
#//		self.toolbar = self.CreateToolBar(wx.TB_HORIZONTAL | wx.NO_BORDER | wx.TB_FLAT | wx.TB_TEXT | wx.MB_DOCKABLE)
		
		
		self.PositionUpdate(self) #update the cursor on run
		# Draw Frame
		self.Show(True)

	def OnNewDoc(self, event):
		newFile = MainWindow(None, -1, "Noober Editor")
		#newFile.Centre()
		newFile.Show()
		
	def OnOpen(self, event): # Open a file
		""" Open a file """
		dlg = wx.FileDialog(self, "Choose a file", self.dirname, "", "*.*", wx.OPEN)
		if dlg.ShowModal() == wx.ID_OK:
			self.filename = dlg.GetFilename()
			self.dirname = dlg.GetDirectory()
			f = open(os.path.join(self.dirname,self.filename),'r')
			self.control.SetValue(f.read())
			self.SetTitle("Noober Editor - " + dlg.GetPath())
			self.FileOpen = 1
			#self.PositionUpdate()
			f.close()
		dlg.Destroy()
		
	def OnSave(self, event): # Save a file 
		""" Save the file to the original file location .... otherwise use saveas """
		if self.FileOpen == 1:
			f = open(os.path.join(self.dirname,self.filename), "w")
			f.write(self.control.GetValue())
			self.SetStatusText("Contents written to " +self.dirname+"\\"+self.filename)
			f.close()
		else:
			self.OnSaveAs(event)
	
	def OnSaveAs(self, event): # Save file to a new path
		""" Save to an arbitrary file and path """
		dlg = wx.FileDialog(self, "Save As", self.dirname, self.filename, "Text Files (*.txt)|*.txt|All Files|*.*", wx.SAVE)
		if (dlg.ShowModal() == wx.ID_OK):
			self.filename = dlg.GetFilename()
			self.dirname = dlg.GetDirectory()
			self.SetTitle("Noober Editor - " + dlg.GetPath())
			self.FileOpen = 1
			self.OnSave(event)
		dlg.Destroy()

	def OnCut(self, event): # Cut selection
		""" Cuts the selection """
		self.control.Cut()
		self.PositionUpdate()
	
	def OnCopy(self, event): # Copy Selection
		""" Copies the selection """
		self.control.Copy()
		self.PositionUpdate()
		
	def OnPaste(self, event): # Paste Selection
		""" Paste the Cut or Copied elements from the clipboard """
		self.control.Paste()
		self.PositionUpdate()
	
	def OnDelete(self, event): # Delete the selection
		""" Deletes the selected portion """
		fromHere, toHere = self.control.GetSelection()
		self.control.Remove(fromHere, toHere)
	
	def OnSelectAll(self, event): # Select the whole document
		""" Select all the contents of the TextCtrl """
		self.control.SelectAll()
		self.PositionUpdate()
	
	def OnAbout(self, event): # Tell the user more about the program
		""" Create a dialog box with information """
		d = wx.MessageDialog(self, "A very simple editor \nwritten in wxPython", "About Noober Editor", wx.OK | wx.ICON_INFORMATION)
		d.ShowModal()
		d.Destroy()
	
	def OnExit(self, event): # exit the program
		""" Kill the program """
		self.Close(True)
	# def OnExit(self, event):
		# if self.modify:
			# dlg = wx.MessageDialog(self, 'Save before Exit?', '', wx.YES_NO | wx.YES_DEFAULT | 
			# wx.CANCEL | wx.ICON_QUESTION)
			# val = dlg.ShowModal()
			# if val == wx.ID_YES:
				# self.OnSaveFile(event)
				# if not self.modify:
					# wx.Exit()
			# elif val == wx.ID_CANCEL:
				# dlg.Destroy()
			# else:
				# self.Destroy()
		# else:
			# self.Destroy()

	def OnSetFont(self, evt): # Set formatting options
		""" Get current font for display and allow to set a custom font """
		dlg = wx.FontDialog(self, wx.FontData())
		dlg.GetFontData().SetInitialFont(self.control.GetFont())
		if dlg.ShowModal() == wx.ID_OK:
			self.control.SetFont(dlg.GetFontData().GetChosenFont())
		dlg.Destroy()
		
	def StatusBarShowTime(self): # Update the string on the status bar ... from above it does this once per second
		""" Shows the time in the bottom right status bar column 2 of [0|1|2] """
		t = time.localtime(time.time())
		#if t.tm_sec % 2 == 0:
		ts = time.strftime("%H:%M   %d %b %Y", t) # day, abbrev. month, year, 24h Min
		#else:
		#	ts = time.strftime("%H %M   %d %b %Y", t) # day, abbrev. month, year, 24h Min
		self.SetStatusText(ts, 2) # third position on the status bar
		
	def TimeStamp(self, event): # appends a time stamp to the document
		""" Inserts a time stamp into the document """
		t = time.localtime(time.time())
		self.control.write(time.strftime("%I:%M %p %d %b %Y\n", t))
		self.PositionUpdate()
		
	def PositionUpdate(self, event): #// Fix this for mouse update as well.
		""" Function to calculate the current position of the cursor and updates the status bar"""
		(posFrom, posTo) = self.control.GetSelection()
		#self.Refresh()
		(col, row) = self.control.PositionToXY(posTo)
		if posFrom == posTo:
			self.SetStatusText(str((row+1, col+1)), 1)
		else:
			delta = posTo - posFrom
			self.SetStatusText(str((row+1, col+1)) + "Sel: " + str(delta), 1)
		self.OldPosTo = posTo
		
	def StatusBarToggle(self, event):
		""" Toggle (show/hide) status bar """
		if self.sb_tog.IsChecked():
			self.statusbar.Show()
		else:
			self.statusbar.Hide()
			
	def ToolBarToggle(self, event):
		""" Toggle (show/hide) toolbar bar """
		if self.tb_tog.IsChecked():
			self.toolbar.Show()
		else:
			self.toolbar.Hide()
	
	def OnPalindromeTest(self, event):
		""" Test the text buffer to see if its contents are a palindrome """
		WhileLoopRun = True
		
		t0 = time.time()
		input = self.control.GetValue().lower()
		
		i = len(input)
		x = 0
		y = i - 1
		
		while x < y and WhileLoopRun:
			ch1 = input[x]
			ch2 = input[y]
			while not ch1.isalnum() and x < y:
				 x += 1
				 ch1 = input[x]
			while not ch2.isalnum() and x < y:
				y -= 1
				ch2 = input[y]
			if ch1 <> ch2:
				t1 = time.time()
				self.SetStatusText("This is NOT a Palindrome!", 0)
				WhileLoopRun = False
			x += 1
			y -= 1
		 
		if WhileLoopRun == True:
			t1 = time.time()
			self.SetStatusText("This is a Palindrome!", 0)
		print self.filename	+ '\n'
		print 'S: %0.4f\nE: %0.4f\nd: %0.4f\n' %  (t0, t1, t1 - t0)
	# def IsLetterOrNumber(x):
		# """ pseudo-code for an idea to check if something is a number or letter """
		# if x >= 'a' and x <= 'z' or x >= 'A' and x <= 'Z' or x >= 0 and x <= 9:
		#	return True
		# else:
		#	return False
		 
	#def OnPalindromeTest(self, event):
		# t0 = time.clock()
		# print t0
		# self.paliTest = self.control.GetValue()
		# p = self.paliTest.lower()
		# p = re.sub(r'\W','',p)
		# while p:
			# if p[:1] == p[-1:]:
				# p = p[1:-1]
			# else:
				# break
		# t1 = time.clock()
		# print t1
		# print 'Palindrome Test took %f  seconds' %  (t1 - t0)
		# if (len(p) <= 1):
			# self.SetStatusText("This is a Palindrome!", 0)
		# else:
			# self.SetStatusText("This is NOT a Palindrome!", 0)
		
		
	
	#// Make it so the program captures if the file could not be opened, try to capture close events to prompt for save, maybe autosave
		

app = wx.App(0)
frame = MainWindow(None, -1, "Noober Editor")
app.MainLoop()